package cn.wolfcode.wolf2w.cloud.controller;

import cn.wolfcode.wolf2w.cloud.feign.UserInfoFeignApi;
import cn.wolfcode.wolf2w.cloud.msg.feign.ISmsFeignApi;
import cn.wolfode.wolf2w.cloud.commom.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//交给spring管理
@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private UserInfoFeignApi userInfoFeignApi;

    @Autowired
    private ISmsFeignApi smsFeignApi;
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        JsonResult result = userInfoFeignApi.checkPhone(phone);

        //result==null;约定result是null，表示走降级方法
        if (result==null){
            return  JsonResult.defaultError();
        }

        //不用去页面提示
        //result.getCode()==200//成功调用
        //result.getCode()!=200//调用异常

        //判断手机号码是否存在
       // boolean ret = userInfoFeignApi.checkPhone(phone);
        return result;


    }

    @GetMapping("/get")
    public Object get(String id){
        JsonResult result = userInfoFeignApi.get(id);
        if (result==null){
            return  JsonResult.defaultError();
        }

        return result;
    }
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode ){
        //先调用msg-api进行短信校验
        JsonResult result=smsFeignApi.checkVerifyCode(phone,verifyCode);
        if (result==null){
            return  JsonResult.defaultError();
        }
        //在调用member-api实现用户注册
        userInfoFeignApi.regist(phone, nickname, password, rpassword);
        return JsonResult.success();
    }

}
